package com.onlyphoto.onlymall.sqlite;


import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by gaochujia on 2019-7-1.
 */

public class OnlyMallSQLiteOpenHelper extends SQLiteOpenHelper {

    private static String name = "only_mall.db";
    //商品表
    public static final String TABLE_PRODUCTS = "products";
    //数据库发生变更时版本号 + 1 才能更新数据库表结构
    public static final int DB_VERSION = 1;

    public OnlyMallSQLiteOpenHelper(Context context) {
        super(context, name, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + TABLE_PRODUCTS + "(id integer primary key autoincrement,name varchar(200), pro_id varchar(200), is_hot integer)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    @Override
    public void onOpen(SQLiteDatabase db) {
        super.onOpen(db);
        //数据库已经存在时不再执行 onCreate(),所以更新表结构时需在此处修改
        if (!db.isReadOnly()){
            //启动外键
            db.execSQL("PRAGMA foreign_keys = 1;");
        }
    }
}
